
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--表单组件-->
      <crudOperation :permission="permission" />

      <el-dialog title="表格配置"
                 :visible.sync="tableConfigShow"
                 width="60%">
        <j-table-column-config :columns.sync="header"  @tabSave="saveHead" />
      </el-dialog>
      <!--表格渲染-->
      <vue-context-menu :contextMenuData="contextMenuData"
                        @tableConfig="tableConfig"
      ></vue-context-menu>
      <el-table ref="table"
                height="700"
                @contextmenu.native="showMenu" show-summary :summary-method="sumRow" @row-click="crud.selectRow"
                v-draghead="manyTableOption" @header-dragend="changeColWidth" stripe border v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号"/>
        <el-table-column v-if="item.visible" v-for="(item, index) in head" :key="`col_${index}`" :prop="item.prop" :label="columnLabel(item)" :width="item.width + 'px'" @header-dragend="changeColWidth" ><template slot-scope='{row,$index}'><span v-if='item.type == 1'>{{ row[`${item.prop}`] }}</span><span v-else-if='item.type == 2' >{{ parseTime(row[`${item.prop}`])}}</span><span v-else-if='item.type == 3' >{{ getDict(`this.dict.label.${item.dictionary}[${row[item.prop]}]`) }}</span><span v-else-if="item.type == 4"></span> </template> </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudReportCustomerReceive from '@/api/erp/reportCustomerReceive'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import JDialog from '../../../components/jDialog/jDialog'
import { getCustomerTypeTree } from '@/api/erp/basCustomerType'
import Treeselect from '@riophae/vue-treeselect'
import { tablemixins } from '../../../mixins/tablemixins'
import { manyTableMixin } from '../../../mixins/manyTableMixin'
import  jTableColumnConfig
  from "@/components/jTableColumnConfig/jTableColumnConfig"
import { getCustomerAreaTree } from '../../../api/erp/basCustomerArea'
import { getDepts } from '../../../api/system/dept'
import headConfig, { reportCustomerReceiveAnalysisDetail } from '../../../headConfig'
// crud交由presenter持有
const defaultCrud = CRUD({ title: '客户应收报表',
  optShow: {
    add: false,
    edit: false,
    del: false,
    download: true,
    leadingIn: false,
    enable: false,
    disable: false,
    pass: false,
    reject: false,
    invalid: false,
    recover: false,
    downloadDetail: false,
    print: false,
    printDetail: false,
  },
  queryType:'post',
  queryOnPresenterCreated:false,
  url: '/api/reportCustomerReceiveAnalysis/list/detail', sort: '', crudMethod: { ...crudReportCustomerReceive }})
const defaultForm = { id: null, salesReceiveAmount: null, plasticBoxAmount: null, returnBoxAmount: null, receiptAmount: null, actualReceiveAmount: null, customerId: null, customerName: null, customerCode: null, fCustomerId: null, fCustomerName: null, fCustomerCode: null }
export default {
  name: 'reportCustomerReceiveAnalysisDetail',
  components: { JDialog, pagination, crudOperation, rrOperation, udOperation, Treeselect, jTableColumnConfig },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),manyTableMixin],
  dicts: ['common_yes_or_no'],
  data() {
    return {
      permission: {
        add: ['admin', 'reportCustomerReceive:add'],
        edit: ['admin', 'reportCustomerReceive:edit'],
        del: ['admin', 'reportCustomerReceive:del'],
        import: ['admin', 'reportCustomerReceive:import'],
        export: ['admin', 'reportCustomerReceive:export'],
        copy: ['admin', 'reportCustomerReceive:copy'],
        upload: ['admin', 'reportCustomerReceive:upload'],
        download: ['admin', 'reportCustomerReceive:download'],
      },
      rules: {
      },
      query:{},
      customerAreaList:[],
      dept:[],
      queryStr:'',
      head:[]
    }
  },
  activated() {
    console.log(typeof this.$route.query.type,this.$route.query.type)
    if (this.$route.query.type){
      const customer = [{ 'dictionary': '', 'label': '客户编码', 'prop': 'customerCode', 'showName': '客户编码', 'sortable': false, 'type': '1', 'visible': true, 'width': 100 }, { 'dictionary': '', 'label': '客户名称', 'prop': 'customerName', 'showName': '客户名称', 'sortable': false, 'type': '1', 'visible': true, 'width': 100 }]
      this.head = [...customer,...headConfig.reportCustomerReceiveAnalysisDetail]
      this.crud.url='/api/reportCustomerReceiveAnalysis/list/detail'
    }else {
      const sCustomer = [{ 'dictionary': '', 'label': '二批编码', 'prop': 'sCustomerCode', 'showName': '二批编码', 'sortable': false, 'type': '1', 'visible': true, 'width': 100 }, { 'dictionary': '', 'label': '二批名称', 'prop': 'sCustomerName', 'showName': '二批名称', 'sortable': false, 'type': '1', 'visible': true, 'width': 100 }]
      this.head = [...sCustomer,...headConfig.reportCustomerReceiveAnalysisDetail]
      this.crud.url='/api/reportSCustomerReceiveAnalysis/list/detail'
    }
    const queryStr = sessionStorage.getItem('reportCustomerReceiveAnalysisDetail')
    if (this.queryStr !== queryStr){
      this.queryStr = queryStr
      this.crud.toQuery()
    }
  },
  created() {
    getCustomerAreaTree().then(res => {
      this.customerAreaList = res.data
    })
    getDepts({status: 1}).then(res => {
      this.deptList = res
    })
  },
  mounted(){
    // this.crud.props.searchToggle = false
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.query = JSON.parse(sessionStorage.getItem('reportCustomerReceiveAnalysisDetail'))
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    onGet() {

    },
    sumRow({ columns, data }){
      const sums = []
      for (let i in  columns) {
        if (['actualAmount', 'balanceAmount', 'beginAmount', 'curAmount','normalAmount','overDateAmount','receiveAmount'].includes(columns[i].property)) {
          let selectSum = 0
          this.$refs.table.selection.forEach(item => {
            selectSum += item[columns[i].property]
          })
          sums[i] = <p>{selectSum}
            <hr/>
            {this.crud.summary[columns[i].property] || 0}</p>
        }
        sums[0] = <p>选中
          <hr/>
          合计</p>
      }
      return sums
    },
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
